package com.zhf.demo03.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/14 19:35
 */
@WebServlet(name = "CookieServlet4",urlPatterns = "/cookie4")
public class CookieServlet4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建cookie信息
        Cookie cookie = new Cookie("name", "zhao");
        //获取cookie默认有效时间
        int maxAge = cookie.getMaxAge();
        System.out.println("cookie的有效时间为："+maxAge);
        //修改cookie有效期
        cookie.setMaxAge(60*10);
        response.addCookie(cookie);
        System.out.println("cookie生命周期设置成功");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
